package com.open.capacity.user.controller;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.user.model.ExcelExport;
import com.open.capacity.user.service.ExcelExportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @author 作者 ftquan E-mail: 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * excel导出管理
 */
@RestController
@Api(tags = "excel导出模块api")
public class ExcelExportController {

    private static Logger logger = LoggerFactory.getLogger(ExcelExportController.class);

    @Autowired
    private ExcelExportService exportService;


    /**
     * 查询excel导出
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('excelExport:get/excelExports')")
    @ApiOperation(value = "查询excel导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "功能菜单Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "String")
    })
    @GetMapping("/excelExports")
    public Result<List<ExcelExport>> findExports(@RequestParam Map<String, Object> params) {
        List<ExcelExport> list = exportService.findExports(params);
        return Result.succeed(list);
    }


    @ApiOperation(value = "获取excel导出")
    @PreAuthorize("hasAuthority('excelExport:get/excelExports/{id}')")
    @GetMapping("/excelExports/{id}")
    public Result<ExcelExport> findExportById(@PathVariable(value = "id") String id) {
        ExcelExport export = exportService.findById(id);
        return Result.succeed(export);
    }

    /**
     * 更新状态
     *
     * @param id
     * @author gitgeek
     */
    @PreAuthorize("hasAuthority('excelExport:post/excelExports/{id}/updateStatus' )")
    @PostMapping(value = "/excelExports/{id}/updateStatus")

    public Result updateStatus(@PathVariable(value = "id") String id, Integer status) {
        exportService.updateStatus(id, status);
        return Result.succeed(null, "更新成功");
    }


    /**
     * excel导出新增或者更新
     *
     * @param export
     * @return
     */
    @PreAuthorize("hasAnyAuthority('excelExport:post/excelExports','excelImport:put/excelExports')")
    @ApiOperation(value = "保存excel导出")
    @PostMapping("/excelExports/saveOrUpdate")

    public Result saveOrUpdate(@RequestBody ExcelExport export, String keyValue) {
        return exportService.saveOrUpdate(export, keyValue);
    }

    /**
     * 删除excel导出
     * delete /excelExports/1
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('excelExport:delete/excelExports/{id}')")
    @ApiOperation(value = "删除excel导出")
    @DeleteMapping("/excelExports/{id}")

    public Result delete(@PathVariable(value = "id") String id) {
        try {
            exportService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {

            return Result.failed("操作失败");
        }
    }

}
